ols_gov_op <- function(dependent_variable,
                       extra_variable,
                       weights){

  with_dems_form <- formula(paste0(dependent_variable,
                                   " ~ ",
                                   extra_variable,
                                   " + ",
                                   paste0(controls_vec, collapse = " + ")))
  model <- lm(with_dems_form,
              data = model_data,
              weights = model_data[[weights]])
  return(model)
}

tidy_predictions <- function(predictions){
    tidy_predictions <- predictions %>%
      rename(term = `at(party_id_gov_op_w19)`) %>%
      make_dwplot_compliant() %>%
      #mutate(spec = "+ demographics")
      left_join(dvars, by = c("model" = "variables")) %>%
      mutate(neat_names = factor(neat_names, levels = dvars$neat_names)) %>%
      mutate(neat_names = factor(case_when(
          neat_names == "Overall" ~ "Overall performance", 
          neat_names == "Overall resp" ~ "Responsibility (overall)", 
          neat_names == "Death toll" ~ "Responsibility (death toll)", 
          neat_names == "Vaccine" ~ "Responsibility (vaccine rollout)", 
          neat_names == "Retro handle" ~ "Retro handle", 
          neat_names == "Change in retro handle" ~ "Change in retro handle"), 
        levels = c("Overall performance",
                   "Responsibility (overall)",
                   "Responsibility (death toll)",
                   "Responsibility (vaccine rollout)",
                   "Retro handle",
                   "Change in retro handle"))) 

  return(tidy_predictions)
}


get_gov_op_predictions <- function(model, dvar){
  predictions <- prediction(model,
    at = list(party_id_gov_op_w19 = c("Government partisan", 
                                      "Opposition partisan",
                                      "Non-partisan"))) %>%
  summary() %>%
  mutate(model = dvar)

  return(predictions)
}

make_dwplot_compliant <- function(tibble_of_results){
  compliant_tibble <- tibble_of_results %>%
    mutate(estimate = Prediction,
           conf.low = lower,
           conf.high = upper) 
  return(compliant_tibble)
}

generic_tidy_texreg <- function(list_of_models, ...){
  nice_outputs <- texreg(list_of_models, 
    use.packages = FALSE,
    booktabs = TRUE,
    dcolumn = TRUE,
    caption.above = TRUE,
    float.pos = "ht!",
    ...)
  
  return(nice_outputs)
}
